/*
* Copyright (c) 2010 The Turing Project
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* Except as contained in this notice, the name(s) of the above
* copyright holders shall not be used in advertising or otherwise to
* promote the sale, use or other dealings in this Software without prior
* written authorization.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
 */

package simulator;
import java.lang.Math.*;

public class Vector3D {
    private Double _component[] = new Double[3];

    public Vector3D(){
        _component[0] = _component[1] = _component[2] = 0.0;
    }

    public void cartesianSet(Double x, Double y, Double z) {
        _component[0] = x;
        _component[1] = y;
        _component[2] = z;
    }

    public void cylinderSet(Double axis, Double radius, Double angle) {
       _component[0] = axis;
       _component[1] = radius * Math.sin(angle);
       _component[2] = radius * Math.cos(angle);
    }

    public void vectorSet(Vector3D newVector) {
        _component[0] = newVector.getX();
        _component[1] = newVector.getY();
        _component[2] = newVector.getZ();

    }

    /**
     * Returns the specified coordinate value of the vector where x = 0, y = 1, and z = 2.
     * @param component
     * @return
     */
    public Double getComponent(int component){
        if(component < 3){
            return _component[component];
        } else {
            return 0.0;
        }
    }

    public Double getX() {
        return _component[0];
    }

    public Double getY() {
        return _component[1];
    }

    public Double getZ() {
        return _component[2];
    }

    public Double getAmplitude() {
        return Math.sqrt(
                Math.pow(_component[0], 2.0) +
                Math.pow(_component[1], 2.0) +
                Math.pow(_component[2], 2.0)
                );
    }

    public void divideBy(Double divisor) {
        for (int i=0; i<3; i++) {
            _component[i] /= divisor;
        }
        //return this;
    }
    
    public void multiplyBy(Double factor) {
        for (int i=0; i<3; i++) {
            _component[i] *= factor;
        }
        //return this;
    }

    public void subtractVector(Vector3D vector) {
        for (int i=0; i<3; i++) {
            _component[i] -= vector.getComponent(i);
        }
        //return this;
    }


    public void addVector(Vector3D vector) {
        for (int i=0; i<3; i++) {
            _component[i] += vector.getComponent(i);
        }
    }

    /**
     * Sets the z component to its absolute value.  This method is used to adjust
     * for collisions with the ground in which a node bounces back in the direction
     * it came from.
     */
    public void absoluteZ() {
       _component[2] = Math.abs(_component[2]);
    }


    public void normalize() {
        Double amplitude = getAmplitude();
        if (amplitude > 0.0) {
            for (int i=0; i<3; i++) {
                _component[i] /= amplitude;
            }
        }
    }

    public Double getDotProduct(Vector3D secondVector) {
        return (_component[0] * secondVector.getComponent(0) +
                _component[1] * secondVector.getComponent(1) +
                _component[2] * secondVector.getComponent(2));
    }
}
